<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="查询日期" prop="queryTime">
        <el-date-picker clearable
                        v-model="queryParams.queryTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        @keyup.enter.native="handleQuery"
                        placeholder="请选择查询日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="房间名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入房间名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomNum">
        <el-input
          v-model="queryParams.roomNum"
          placeholder="请输入房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="可住人数" prop="peopleNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.peopleNum"-->
<!--          placeholder="请输入可住人数"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房间名" align="center" prop="name" />
      <el-table-column label="类型" align="center" prop="typeName" />
      <el-table-column label="房间号" align="center" prop="roomNum" />
      <el-table-column label="状态" align="center" prop="statusName" />
      <el-table-column label="房间价格" align="center" prop="roomPrice" />
      <el-table-column label="房间大小" align="center" prop="roomSize" />
      <el-table-column label="可住人数" align="center" prop="peopleNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客房信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="8">
          <el-form-item label="房间号" prop="roomNum">
            <el-input v-model="form.roomNum" placeholder="请输入房间号" :disabled="detailDisabled"/>
          </el-form-item>
          </el-col>
            <el-col :span="8">
          <el-form-item label="房间价格" prop="roomPrice">
            <el-input v-model="form.roomPrice" placeholder="请输入房间价格"  :disabled="detailDisabled"/>
          </el-form-item>
            </el-col>
              <el-col :span="8">
                <el-form-item label="床位信息" prop="bedInfo">
                  <el-input v-model="form.bedInfo" placeholder="请输入床位信息"  :disabled="detailDisabled"/>
                </el-form-item>
              </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="房间大小" prop="roomSize">
              <el-input v-model="form.roomSize" placeholder="请输入房间大小"  :disabled="detailDisabled"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择类型" :disabled="detailDisabled">
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
<!--          <el-col :span="6">-->
<!--            <el-form-item label="状态" prop="status">-->
<!--              <el-select v-model="form.status" placeholder="请选择状态" :disabled="detailDisabled">-->
<!--                <el-option-->
<!--                  v-for="item in statusList"-->
<!--                  :key="item.value"-->
<!--                  :label="item.label"-->
<!--                  :value="item.value">-->
<!--                </el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="8">
            <el-form-item label="可住人数" prop="peopleNum">
              <el-input-number  v-model="form.peopleNum" :min="1" :max="10" label="请输入可住人数" :disabled="detailDisabled"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="便利设施" prop="amenity">
              <el-input type="textarea" :rows="2" v-model="form.amenity" placeholder="请输入便利设施" width="480px" :disabled="detailDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="浴室配套" prop="bathroomAccessory">
              <el-input type="textarea" :rows="2" v-model="form.bathroomAccessory" placeholder="请输入浴室配套" :disabled="detailDisabled" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="洗浴用品" prop="bathingSupply">
              <el-input type="textarea" :rows="2" v-model="form.bathingSupply" placeholder="请输入洗浴用品" :disabled="detailDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="食品饮品" prop="food">
              <el-input type="textarea" :rows="2" v-model="form.food" placeholder="请输入食品饮品" :disabled="detailDisabled" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="媒体科技" prop="media">
              <el-input type="textarea" :rows="2" v-model="form.media" placeholder="请输入媒体科技" :disabled="detailDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他信息" prop="other">
              <el-input type="textarea" :rows="2" v-model="form.other" placeholder="请输入其他信息" :disabled="detailDisabled" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="是否提供wifi" prop="wifi">
              <el-radio v-model="form.wifi" label="是" :disabled="detailDisabled">是</el-radio>
              <el-radio v-model="form.wifi" label="否" :disabled="detailDisabled">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否有窗" prop="casement">
              <el-radio v-model="form.casement" label="是" :disabled="detailDisabled">是</el-radio>
              <el-radio v-model="form.casement" label="否" :disabled="detailDisabled">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否可吸烟" prop="smoke">
              <el-radio v-model="form.smoke" label="是" :disabled="detailDisabled">是</el-radio>
              <el-radio v-model="form.smoke" label="否" :disabled="detailDisabled">否</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否包含早餐" prop="breakfast">
              <el-radio v-model="form.breakfast" label="是" :disabled="detailDisabled">是</el-radio>
              <el-radio v-model="form.breakfast" label="否" :disabled="detailDisabled">否</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="图片" prop="picture">
              <image-upload v-model="form.picture" :disabled="detailDisabled"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoom, getRoom, delRoom, addRoom, updateRoom } from "@/api/system/room";

export default {
  name: "Room",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客房信息表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态
      statusList: [{
        value: 1,
        label: '空闲'
      }, {
        value: 2,
        label: '已预约'
      }, {
        value: 3,
        label: '已入住'
      }, {
        value: 4,
        label: '待清洁'
      }],
      typeList: [{
        value: 1,
        label: '豪华大床房'
      }, {
        value: 2,
        label: '豪华标准间'
      }, {
        value: 3,
        label: '套房'
      }, {
        value: 4,
        label: '家庭房'
      }, {
        value: 5,
        label: '大床房'
      }, {
        value: 6,
        label: '普通标间'
      }, {
        value: 7,
        label: '钟点房'
      }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        roomNum: null,
        type: null,
        picture: null,
        status: null,
        roomPrice: null,
        roomSize: null,
        amenity: null,
        bathroomAccessory: null,
        bathingSupply: null,
        peopleNum: null,
        bedInfo: null,
        wifi: null,
        casement: null,
        smoke: null,
        breakfast: null,
        food: null,
        media: null,
        other: null,
        queryTime:null,
        createUser: null,
        updateUser: null,
        isDeleted: null
      },
      // 表单参数
      form: {},
      detailDisabled: false,
      // 表单校验
      rules: {
        roomNum: [
          { required: true, message: "房间号不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        picture: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        roomPrice: [
          { required: true, message: "房间价格不能为空", trigger: "blur" }
        ],
        roomSize: [
          { required: true, message: "房间大小不能为空", trigger: "blur" }
        ],
        amenity: [
          { required: true, message: "便利设施不能为空", trigger: "blur" }
        ],
        bathroomAccessory: [
          { required: true, message: "浴室配套不能为空", trigger: "blur" }
        ],
        bathingSupply: [
          { required: true, message: "洗浴用品不能为空", trigger: "blur" }
        ],
        peopleNum: [
          { required: true, message: "可住人数不能为空", trigger: "blur" }
        ],
        bedInfo: [
          { required: true, message: "床位信息不能为空", trigger: "blur" }
        ],
        wifi: [
          { required: true, message: "是否提供wifi不能为空", trigger: "blur" }
        ],
        casement: [
          { required: true, message: "是否有窗不能为空", trigger: "blur" }
        ],
        smoke: [
          { required: true, message: "是否可吸烟不能为空", trigger: "blur" }
        ],
        breakfast: [
          { required: true, message: "是否包含早餐不能为空", trigger: "blur" }
        ],
        food: [
          { required: true, message: "食品饮品不能为空", trigger: "blur" }
        ],
        media: [
          { required: true, message: "媒体科技不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客房信息列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        roomNum: null,
        type: null,
        picture: null,
        status: null,
        roomPrice: null,
        roomSize: null,
        amenity: null,
        bathroomAccessory: null,
        bathingSupply: null,
        peopleNum: null,
        bedInfo: null,
        wifi: null,
        casement: null,
        smoke: null,
        breakfast: null,
        food: null,
        media: null,
        other: null,
        createUser: null,
        createTime: null,
        updateUser: null,
        updateTime: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.detailDisabled=false;
      this.title = "添加客房信息";
    },
    /** 详情 */
    handleDetail(row){
      getRoom(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看客房信息";
        this.detailDisabled=true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRoom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客房信息";
      });
      this.detailDisabled=false;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客房信息编号为"' + ids + '"的数据项？').then(function() {
        return delRoom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
